<?php

namespace app\services\ad\monitor;

use app\datalog\AdMonitorLog;
use app\model\sdk\log\LogAdMonitor;
use app\model\sdk\log\LogAdMonitorInfo;
use support\Request;

// 今日头条-应用推广
class ToutiaoApp extends MonitorBase
{
    // 此接口由子类继承
    // 处理落地监控数据
    public function handleMonitorInfo(Request $request)
    {
        $osList = ['0' => 1, '1' => 2, '3' => 3];
        // 待匹配日志
        $params           = $request->all();
        $log              = LogAdMonitor::suffix(date('Ym'));
        $log->create_time = time();
        $log->plan_id     = $params['plan_id'];
        $log->imei        = $params['imei'] ?? '';
        $log->idfa        = $params['idfa'] ?? '';
        $log->android_id  = $params['android_id'] ?? '';
        $log->oaid        = $params['oaid'] ?? '';
        $log->device_type = $osList[$params['os'] ?? 3] ?? 0;
        $log->ip          = $params['ip'] ?? '';
        if ($log->device_type == 1) {
            $device_num = $params['android_id'] ?? '';
        } else {
            $device_num = $params['idfa'] ?? '';
        }
        $log->device_num  = $device_num;
        $log->click_id    = $params['request_id'] ?? '';
        $log->save();

        // 完整日志
        $info              = LogAdMonitorInfo::suffix(date('Ym'));
        $info->create_time = time();
        $info->plan_id     = $params['plan_id'];
        $info->monitor_id  = $log->id;
        $info->data        = json_encode($params, JSON_UNESCAPED_UNICODE);
        $info->save();

        // 落地日志
        AdMonitorLog::getInstance()->addLog($log, $info);

        return yqok();
    }
}
